package com.atguigu.tingshu.order.strategy.Impl;

import com.atguigu.tingshu.common.constant.SystemConstant;
import com.atguigu.tingshu.common.result.Result;
import com.atguigu.tingshu.model.user.VipServiceConfig;
import com.atguigu.tingshu.order.strategy.OrderTradeStrategy;
import com.atguigu.tingshu.user.client.VipServiceConfigFeignClient;
import com.atguigu.tingshu.vo.order.OrderDerateVo;
import com.atguigu.tingshu.vo.order.OrderDetailVo;
import com.atguigu.tingshu.vo.order.OrderInfoVo;
import com.atguigu.tingshu.vo.order.TradeVo;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.util.Arrays;

@Slf4j
@Service
public class VipOrderTradeStrategy implements OrderTradeStrategy {

    @Autowired
    private VipServiceConfigFeignClient vipServiceConfigFeignClient ;

    @Autowired
    private RedisTemplate<String , String> redisTemplate ;

    @Override
    public OrderInfoVo trade(TradeVo tradeVo) {

        // 根据付款项目的id查询vip的配置信息
        Long itemId = tradeVo.getItemId();
        Result<VipServiceConfig> serviceConfigResult = vipServiceConfigFeignClient.findById(itemId);
        VipServiceConfig vipServiceConfig = serviceConfigResult.getData();

        // 创建一个OrderInfoVo对象，封装结算页所需要的数据
        OrderInfoVo orderInfoVo = new OrderInfoVo() ;
        orderInfoVo.setItemType(SystemConstant.ORDER_ITEM_TYPE_VIP);

        // 设置相关金额数据
        BigDecimal originalAmount = vipServiceConfig.getPrice();
        BigDecimal derateAmount = vipServiceConfig.getDiscountPrice().subtract(vipServiceConfig.getDiscountPrice());
        BigDecimal orderAmount = vipServiceConfig.getDiscountPrice();
        orderInfoVo.setOriginalAmount(originalAmount);
        orderInfoVo.setDerateAmount(derateAmount);
        orderInfoVo.setOrderAmount(orderAmount);

        // 构建明细数据
        OrderDetailVo orderDetailVo = new OrderDetailVo() ;
        orderDetailVo.setItemId(vipServiceConfig.getId());
        orderDetailVo.setItemName(vipServiceConfig.getName());
        orderDetailVo.setItemUrl(vipServiceConfig.getImageUrl());
        orderDetailVo.setItemPrice(orderAmount);
        orderInfoVo.setOrderDetailVoList(Arrays.asList(orderDetailVo));

        // 订单的减免明细
        if(orderInfoVo.getDerateAmount().intValue() > 0) {

            OrderDerateVo orderDerateVo = new OrderDerateVo() ;     // 减免明细对象
            orderDerateVo.setDerateType(SystemConstant.ORDER_DERATE_VIP_SERVICE_DISCOUNT);
            orderDerateVo.setDerateAmount(derateAmount);
            orderDerateVo.setRemarks("vip折扣");
            orderInfoVo.setOrderDerateVoList(Arrays.asList(orderDerateVo));

        }

        this.setOtherDataToOrderInfoVo(orderInfoVo , redisTemplate);

        // 返回
        return orderInfoVo;
    }

    @Override
    public String getItemType() {
        return SystemConstant.ORDER_ITEM_TYPE_VIP;  // 1003
    }

}
